package commander;

import java.io.*;



import core.*;

import repository.Repository;



public class CLI {

	/**
	 * Command 'jit init'
	 *
	 * @param args
	 * @throws IOException
	 */
	public static void jitInit(String[] args) throws IOException {
		String path = "";
		if (args.length <= 2) { //get default working path
			path = new File(".").getCanonicalPath();
			JitInit.init(path);
		} else if (args[2].equals("-help")) { //see help
			System.out.println("usage: jit init [<path>] [-help]\r\n" +
					"\r\n" +
					"jit init [<path>]:	Create an empty jit repository or reinitialize an existing one in the path or your default working directory.");
		} else {
			path = args[2];
			if (!new File(path).isDirectory()) { //if the working path input is illegal
				System.out.println(path + "is not a legal directory. Please init your reposiroty again. See 'jit init -help'.");
			} else {
				JitInit.init(path);
			}
		}
	}


	/**
	 * Command 'jit help'.
	 */
	public static void jitHelp() {
		System.out.println("usage: jit [--version] [--help] [-C <path>] [-c name=value]\r\n" +
				"           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\r\n" +
				"           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\r\n" +
				"           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\r\n" +
				"           <command> [<args>]\r\n" +
				"\r\n" +
				"These are common Jit commands used in various situations:\r\n" +
				"\r\n" +
				"start a working area\r\n" +
				"   init       Create an empty Jit repository or reinitialize an existing one\r\n" +
				"\r\n" +
				"work on the current change\r\n" +
				"   add        Add file contents to the index\r\n" +
				"   reset      Reset current HEAD to the specified state\r\n" +
				"   rm         Remove files from the working tree and from the index\r\n" +
				"\r\n" +
				"examine the history and state\r\n" +
				"   log        Show commit logs\r\n" +
				"   status     Show the working tree status\r\n" +
				"\r\n" +
				"grow, mark and tweak your common history\r\n" +
				"   branch     List, create, or delete branches\r\n" +
				"   checkout   Switch branches or restore working tree files\r\n" +
				"   commit     Record changes to the repository\r\n" +
				"   diff       Show changes between commits, commit and working tree, etc\r\n" +
				"   merge      Join two or more development histories together\r\n" +
				"\r\n" +
				"'jit help -a' and 'jit help -g' list available subcommands and some\r\n" +
				"concept guides. See 'jit help <command>' or 'jit help <concept>'\r\n" +
				"to read about a specific subcommand or concept.");
	}

	/**
	 * Command 'jit add'
	 *
	 * @param args
	 * @throws IOException
	 */
	public static void jitAdd(String[] args) throws Exception {
		String filepath = "";
		if (args.length <= 2) {
			System.out.println("It is not a git command. See 'git help'.");
			jitHelp();
		} else if (args[2].equals("-help")) { //Add all files at once
			System.out.println("usage: jit add <file1> [<file2>...] [-help]\r\n" +
					"jit add <file1> [<file2>...]: Add file(s) to stage.");
		} else {
			for (int i = 2; i < args.length; i++) {
				filepath = args[i];
				JitAdd.add(filepath);
			}
		}
	}

	public static void jitRm(String[] args) throws Exception {
		String filepath = "";
		if (args.length <= 2) {
			System.out.println("It is not a git command. See 'git help'.");
			jitHelp();
		} else if (args[2].equals("-help")) { //Add all files at once
			System.out.println("usage: jit rm <file1> [<file2>...] [-help]\r\n" +
					"jit rm <file1> [<file2>...]: Remove file(s) from stage.");
		} else {
			for (int i = 2; i < args.length; i++) {
				filepath = args[i];
				JitRm.rm(filepath);
			}
		}
	}

	public static void jitCommit(String[] args) throws Exception {
		if (args[1].equals("commit") && args[2].equals("-m")) {
			if (args.length > 3) {
				JitCommit.commit(args[3]);
			} else {
				JitCommit.commit(" ");
			}
		} else {
			System.out.println("It is not a git command. See 'git help'.");
		}
	}

	public static void jitLog(String[] args) throws Exception {
		if (args.length < 2) {
			System.out.println("It is not a git command. See 'git help'.");
			jitHelp();
		} else if (args.length == 3 && args[2].equals("-help")) { //Add all files at once
			System.out.println("usage: jit log [-help]\r\n" +
					"jit log: print log of commit history");
		} else {
			JitLog.log();
		}
	}

	public static void jitBranch(String[] args) throws Exception {
		if (args.length < 3) {
			System.out.println("It is not a git command. See 'git help'.");
			jitHelp();
		} else if (args.length == 3 && args[2].equals("-help")) { //Add all files at once
			System.out.println("usage: jit branch [-help]\r\n" +
					"jit branch: creat new branch");
		} else {
			JitBranch.branch(args[2]);
		}
	}

	public static void jitCheckout(String[] args) throws IOException {
		String name = "";
		if (args.length <= 2) {
			System.out.println("It is not a git command. See 'git help'.");
			jitHelp();
		} else if (args.length == 4 && args[2].equals("-b")) {
			name = args[3];
			JitBranch.branch(name);
			JitCheckout.checkout(name);
		} else {
			name = args[2];
			JitCheckout.checkout(name);
		}
	}

	public static void jitReset(String[] args) throws Exception {
		String mode = "";
		String commitId = "";
		if (args.length <= 3) {
			System.out.println("It is not a git command. See 'git help'.");
			jitHelp();
		} else if (args[2].equals("-help")) {
			System.out.println("usage: jit reset [<mode>] [<commit>] [-help]\r\n" +
					"jit reset <mode> <commit> : Reset current HEAD to the specifiedd state.");
		} else {
			mode = args[2];
			commitId = args[3];
			JitReset.reset(mode, commitId);
		}
	}


	public static void main(String[] args) throws Exception {
		File repository = new File("." + File.separator + "repository" + File.separator + "repoPathFile");
		if(repository.exists()) {
			FileInputStream is = new FileInputStream("." + File.separator + "repository" + File.separator + "repoPathFile");
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String repositoryPath = br.readLine();//读取仓库地址文件内容
			Repository.setWorkTree(repositoryPath);
			Repository.setGitDir(repositoryPath);
		}

		if(args.length <= 1 || args[1].equals("help")) {
			jitHelp();
		}else {
			switch(args[1]){
				case "init":
					jitInit(args);
					break;
				case "add":
					jitAdd(args);
					break;
				case "rm":
					jitRm(args);
					break;
				case "commit":
					jitCommit(args);
					break;
				case "log":
					jitLog(args);
					break;
				case "branch":
					jitBranch(args);
					break;
				case "checkout":
					jitCheckout(args);
					break;
				case "reset":
					jitReset(args);
				default:
					System.out.println("jit: " + args[1] + "is not a git command. See 'git help'.");
			}
		}
	}
}